
// <copyright file="DuplicatesFilteredEnumerator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The duplicates filtered enumerator.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Generic
{
    using System.Collections;
    using System.Collections.Generic;

    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    /// The duplicates filtered enumerator.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public sealed class DuplicatesFilteredEnumerator<T> : IEnumerator<T>
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly IEnumerator<T> adaptedObject;

        /// <summary>
        /// The hash set.
        /// </summary>
        private readonly HashSet<T> _hashSet;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DuplicatesFilteredEnumerator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        public DuplicatesFilteredEnumerator(IEnumerator<T> adaptedObject)
        {
            Guard.ArgumentNotNull(adaptedObject, "adaptedObject");
            this.adaptedObject = adaptedObject;
            this._hashSet = new HashSet<T>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DuplicatesFilteredEnumerator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        /// <param name="comparer">
        /// The comparer.
        /// </param>
        public DuplicatesFilteredEnumerator(IEnumerator<T> adaptedObject, IEqualityComparer<T> comparer)
        {
            Guard.ArgumentNotNull(adaptedObject, "adaptedObject");
            Guard.ArgumentNotNull(comparer, "comparer");
            this.adaptedObject = adaptedObject;
            this._hashSet = new HashSet<T>(comparer);
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        public T Current
        {
            get
            {
                return this.adaptedObject.Current;
            }
        }

        #endregion Public Properties

        #region Explicit Interface Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        object IEnumerator.Current
        {
            get
            {
                return this.adaptedObject.Current;
            }
        }

        #endregion Explicit Interface Properties

        #region Public Methods and Operators

        /// <summary>
        /// The dispose.
        /// </summary>
        public void Dispose()
        {
            this.adaptedObject.Dispose();
            this._hashSet.Clear();
        }

        /// <summary>
        /// The move next.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool MoveNext()
        {
            while (this.adaptedObject.MoveNext())
            {
                if (this._hashSet.Add(this.adaptedObject.Current))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// The reset.
        /// </summary>
        public void Reset()
        {
            this.adaptedObject.Reset();
            this._hashSet.Clear();
        }

        #endregion Public Methods and Operators
    }
}